package com.zksk.portal.controller.policy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.api.RemoteDictService;
import com.zksk.admin.api.RemotePolicyService;
import com.zksk.admin.api.domain.SysDictData;
import com.zksk.admin.api.domain.portal.vo.PolicyInfoVo;
import com.zksk.common.core.constant.PolicyInfoConstants;
import com.zksk.common.core.constant.SecurityConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.PolicyInfo;
import com.zksk.portal.domain.vo.LeftNavMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 政策资讯
 *
 * @author zksk
 */
@Slf4j
@RestController
@RequestMapping("/policy")
@Api(value = "PolicyInfoController", tags = "政策资讯相关接口")
public class PolicyInfoController extends BaseController {


    @Autowired
    private RemotePolicyService remotePolicyService;

    @Autowired
    private RemoteDictService remoteDictService;

    /**
     * 政策资讯左侧菜单
     * @return
     */
    @GetMapping("/nav")
    @ApiOperation(value = "获取政策资讯左侧菜单")
    public R<List<LeftNavMenuVo>> navMenu(){
        List<SysDictData> dictDataByCode = remoteDictService
                .getDictDataByCode(PolicyInfoConstants.POLICY_DICT_CODE, SecurityConstants.INNER).getData();

        List<LeftNavMenuVo> resultList = dictDataByCode.stream().map(item -> {
            LeftNavMenuVo leftNavMenuVo = new LeftNavMenuVo();
            leftNavMenuVo.setValue(item.getDictValue());
            leftNavMenuVo.setLabel(item.getDictLabel());
            leftNavMenuVo.setType(item.getDictType());
            leftNavMenuVo.setDesc(item.getRemark());
            return leftNavMenuVo;
        }).collect(Collectors.toList());

        return R.ok(resultList,"菜单获取成功");
    }

    @Log(title = "政策资讯查询",businessType = BusinessType.SELECT)
    @PostMapping("/list")
    @ApiOperation(value = "政策资讯查询")
    public R<Page<PolicyInfo>> list(@RequestBody PolicyInfoVo policyInfoVo){
        R<Page<PolicyInfo>> pageR = remotePolicyService.list(policyInfoVo, SecurityConstants.INNER);
        if (pageR.getCode() == R.SUCCESS) {
            Page<PolicyInfo> policyInfoPage=pageR.getData();
            return R.ok(policyInfoPage, "获取分页列表信息成功");
        }
        return R.fail("获取分页列表信息失败");

    }

    /**
     * 根据政策咨询id查询资讯信息
     * @param id
     * @return
     */
    @Log(title = "根据政策咨询id查询资讯信息",businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据政策咨询id查询资讯信息")
    public R<PolicyInfo> select(@PathVariable Long id) {
        R<PolicyInfo> policyInfoR = remotePolicyService.select(id, SecurityConstants.INNER);
        if (policyInfoR.getCode()==R.SUCCESS){
            PolicyInfo policyInfoRData = policyInfoR.getData();
            return R.ok(policyInfoRData,"查询详情信息成功");
        }
        return R.fail("查询详情信息失败");
    }





}
